<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="等级名称"
          :label-col="labelCol"
          :wrapper-col="wrapperCol">
          <a-input
            allowClear
            placeholder="请输入等级名称"
            v-decorator="['level_name', {rules: [{required: true, message: '请输入等级名称'}]}]"
          />
        </a-form-item>
        <a-form-item label="绑定题库" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            :filter-option="filterOption"
            allowClear
            show-search
            v-decorator="['question_id', {rules: [{required: true, message: '请选择需要绑定的题库'}]}]"
            placeholder="请选择需要绑定的题库"
          >
            <a-select-option v-for="item in questionList" :value="item.question_id">{{ item.question_name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import * as Api from '@/api/subject/level'
import * as questionApi from '@/api/question/index'
import _ from 'lodash'

export default {
  components: {},
  props: ['subject_id'],
  data () {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {},
      // 当前模式 add新增 edit编辑
      mode: '',
      questionList: []
    }
  },
  methods: {
    // 多选框搜索配置
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    /**
     * 显示对话框
     */
    add () {
      this.mode = 'add'
      this.title = '添加等级'
      this.visible = true
      this.record = {}
      this.getQuestionList()
    },
    edit (record) {
      this.mode = 'edit'
      this.title = '编辑等级'
      this.visible = true
      this.record = record
      this.setFieldsValue()
      this.getQuestionList()
    },

    async getQuestionList () {
      const { data: { list } } = await questionApi.all({ status: 0 })
      this.questionList = list
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        const data = _.pick(this.record, ['level_name', 'question_id'])
        setFieldsValue(data)
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      const { form: { validateFields } } = this
      // 表单验证
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true
      var sendApi = null
      if (this.mode == 'add') {
        values.subject_id = this.subject_id
        sendApi = Api.add({ form: values })
      } else {
        sendApi = Api.edit({ apiId: this.record['subject_level_id'], form: values })
      }
      sendApi.then((result) => {
        // 显示成功
        this.$message.success(result.message, 1.5)
        // 关闭对话框
        this.handleCancel()
        // 通知父端组件提交完成了
        this.$emit('handleSubmit', values)
      })
        .finally((result) => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
<style scoped lang="less">
.table {
  width: 100%;
  border-color: #e8e8e8;
  text-align: center;

  td {
    width: 33.333%;
  }
}
</style>
